package com.kyomonn.talkischeap.PersonalCenter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.imageview.ShapeableImageView;
import com.kyomonn.talkischeap.LoginActivity;
import com.kyomonn.talkischeap.MySQLiteOpenHelper;
import com.kyomonn.talkischeap.R;

import java.util.Objects;

public class PersonalCenterActivity extends AppCompatActivity {
    private TextView tvUsername;
    private ShapeableImageView ivAvatar;
    private Toolbar toolbar;
    private SharedPreferences sharedPref;
    private MySQLiteOpenHelper mySQLiteOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_personal_center);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.activity_personal_center), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        sharedPref = getSharedPreferences("user_data", Context.MODE_PRIVATE);
        mySQLiteOpenHelper = new MySQLiteOpenHelper(this, "LocalStorage.db", null, 1);

        initViews();
        setOnClickListeners();
    }

    private void initViews() {
        // 绑定视图元素
        toolbar = findViewById(R.id.toolbar);
        tvUsername = findViewById(R.id.tv_username);
        ivAvatar = findViewById(R.id.iv_headshot_personal_activity);

        // 设置 Toolbar
        setSupportActionBar(toolbar);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); // 显示返回箭头

        // 设置默认值
        tvUsername.setText(sharedPref.getString("username", ""));
        ivAvatar.setImageResource(R.mipmap.ic_launcher);
    }

    private void setOnClickListeners() {
        SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_custom, null);
        EditText etUsername = dialogView.findViewById(R.id.et_dialog);

        // 用户昵称点击事件
        findViewById(R.id.btn_username).setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("修改用户名");
            etUsername.setHint("请输入新用户名");
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setView(dialogView);
            builder.setPositiveButton("确定", (dialog, which) -> {
                String username = etUsername.getText().toString();
                String email = sharedPref.getString("email", "");

                String sql = "UPDATE users SET username = ? WHERE email = ?;";
                db.execSQL(sql, new String[]{username, email});
                db.close();

                sharedPref.edit().putString("username", username).apply();

                tvUsername.setText(username);

                Toast.makeText(this, "修改成功！", Toast.LENGTH_SHORT).show();
            });
            builder.setNegativeButton("取消", null);
            builder.show();
        });

        // 头像点击事件
        findViewById(R.id.btn_headshot).setOnClickListener(v -> {

        });

        // 退出登录按钮点击事件
        findViewById(R.id.btn_logout).setOnClickListener(v -> {
            SharedPreferences sharedPref = getSharedPreferences("user_data", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            editor.putBoolean("is_login", false);
            editor.apply();
            startActivity(new Intent(this, LoginActivity.class));
        });
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
}